package com.ly.talent.controller;

import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.core.domain.entity.SysDictData;
import com.ly.common.core.page.TableDataInfo;
import com.ly.common.enums.BusinessType;
import com.ly.common.utils.ServletUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.poi.ExcelUtil;
import com.ly.framework.web.service.TokenService;
import com.ly.system.service.ISysDictTypeService;
import com.ly.talent.domain.TalentBaseInfo;
import com.ly.talent.domain.TalentUniversity;
import com.ly.talent.domain.vo.TalentUniversityVo;
import com.ly.talent.service.ITalentBaseInfoService;
import com.ly.talent.service.ITalentUniversityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 人才引进基本信息Controller
 * 
 * @author sfli
 * @date 2021-02-01
 */
@Api("人才引进申报信息")
@RestController
@RequestMapping("wx/talent/")
public class WxTalentBaseInfoController extends BaseController
{
    @Autowired
    private ITalentBaseInfoService   talentBaseInfoService;
    @Autowired
    private TokenService             tokenService;
    @Autowired
    private ISysDictTypeService      dictTypeService;
    @Autowired
    private ITalentUniversityService talentUniversityService;
    /**
     * 获取人才引进基本信息详细信息
     */
    @ApiOperation("获取人才引进基本信息详细信息，需要登陆后获取")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo()
    {
        String openId = tokenService.getWxOpenId(ServletUtils.getRequest());
        return AjaxResult.success(talentBaseInfoService.selectTalentByOpenId(openId));
    }
    /**
     * 新增人才引进基本信息
     */
    @ApiOperation("新增人才引进基本信息")
    @Log(title = "人才引进基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TalentBaseInfo talentBaseInfo)
    {
        //小程序用户id
        String openId = tokenService.getWxOpenId(ServletUtils.getRequest());
        //查看用户是否已经提交过
        TalentBaseInfo old = talentBaseInfoService.selectTalentByOpenId(openId);
        if(old!=null){
            talentBaseInfo.setCreateBy(openId);
            talentBaseInfo.setTalentBaseInfoId(old.getTalentBaseInfoId());
            talentBaseInfo.getTalentDetail().setTalentDetailId(old.getTalentDetail().getTalentDetailId());
            talentBaseInfo.getTalentDetail().setTalentBaseInfoId(old.getTalentBaseInfoId());
            talentBaseInfoService.updateTalentBaseInfo(talentBaseInfo);
            return AjaxResult.success(talentBaseInfo);
        }
        talentBaseInfo.setCreateBy(openId);
        talentBaseInfo.getTalentDetail().setCreateBy(openId);
        talentBaseInfoService.insertTalentBaseInfo(talentBaseInfo);
        return AjaxResult.success(talentBaseInfo);
    }
    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("根据字典类型查询字典数据信息")
    @GetMapping(value = "/dict/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if ( StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }


    /**
     * 查询学校信息列表
     */
    @ApiOperation("国内一流学校")
    @GetMapping("/university/china")
    public AjaxResult chinaList(TalentUniversityVo talentUniversityVo)
    {
        TalentUniversity talentUniversity = new TalentUniversity();
        talentUniversity.setType("china");
        talentUniversity.setName(talentUniversityVo.getName());
        talentUniversity.setNameEn(talentUniversityVo.getNameEn());
        List<TalentUniversity> list = talentUniversityService.selectTalentUniversityList(talentUniversity);
        if ( StringUtils.isNull(list))
        {
            list = new ArrayList<TalentUniversity>();
        }
        return AjaxResult.success(list);
    }
    /**
     * 查询学校信息列表
     */
    @ApiOperation("QS排名")
    @GetMapping("/university/qs")
    public AjaxResult qsList(TalentUniversityVo talentUniversityVo)
    {
        TalentUniversity talentUniversity = new TalentUniversity();
        talentUniversity.setName(talentUniversityVo.getName());
        talentUniversity.setNameEn(talentUniversityVo.getNameEn());
        talentUniversity.setType("QS");
        List<TalentUniversity> list = talentUniversityService.selectTalentUniversityList(talentUniversity);
        if ( StringUtils.isNull(list))
        {
            list = new ArrayList<TalentUniversity>();
        }
        return AjaxResult.success(list);
    }
    /**
     * 查询学校信息列表
     */
    @ApiOperation("THE排名")
    @GetMapping("/university/the")
    public AjaxResult theList(TalentUniversityVo talentUniversityVo)
    {
        TalentUniversity talentUniversity = new TalentUniversity();
        talentUniversity.setName(talentUniversityVo.getName());
        talentUniversity.setNameEn(talentUniversityVo.getNameEn());
        talentUniversity.setType("THE");
        List<TalentUniversity> list = talentUniversityService.selectTalentUniversityList(talentUniversity);
        if ( StringUtils.isNull(list))
        {
            list = new ArrayList<TalentUniversity>();
        }
        return AjaxResult.success(list);
    }
    /**
     * 查询学校信息列表
     */
    @ApiOperation("US NEWS排名")
    @GetMapping("/university/us")
    public AjaxResult usList(TalentUniversityVo talentUniversityVo)
    {
        TalentUniversity talentUniversity = new TalentUniversity();
        talentUniversity.setName(talentUniversityVo.getName());
        talentUniversity.setNameEn(talentUniversityVo.getNameEn());
        talentUniversity.setType("US");
        List<TalentUniversity> list = talentUniversityService.selectTalentUniversityList(talentUniversity);
        if ( StringUtils.isNull(list))
        {
            list = new ArrayList<TalentUniversity>();
        }
        return AjaxResult.success(list);
    }
}
